﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Soft000.Model;
using Soft000.Common;
using Soft000.IDAL;
using Soft000.DataFactory;
using Soft000.Config;

public partial class MasterPage : System.Web.UI.MasterPage
{
    public IOnLine m_Online = Factory.CreateOnLine();
    public IHome m_Home = Factory.CreateHome();
    public IStylecss m_Style = Factory.CreateStyleCss();
    public ITotal m_Total = Factory.CreateTotal();
    public IUser m_User = Factory.CreateUser();
    #region styles
    public string background, hColor, tColor, tColor1, tColor2, bdtj, bdtj2;
    public string tdc, tdc1, tdc2, tdc3, top1, link, topimg, r_reply, t_reply, v_reply;
    #endregion
    public HomeInfo m_HomeInfo = new HomeInfo();
    public TotalInfo m_TotalInfo = new TotalInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        UpdateOnline();
        CheckIp();m_HomeInfo.l_Title
        LoadStyle();
        HomeInfo hInfo = m_Home.GetHomeInfo();
        if (hInfo != null)
        {
            m_HomeInfo = hInfo;
        }
        TotalInfo tInfo = m_Total.GetTotalInfo();
        if (tInfo != null)
        {
            m_TotalInfo = tInfo;
        }
        


    }
    public void IsLogin()
    {
        if (string.IsNullOrEmpty(Request.Cookies["soft000"]["UserName"]))
        {
            //游客
            spLogin.Visible = true;
            spModifyUserInfo.Visible = false;
        }
        else
        {
            //已登录
            spLogin.Visible =false ;
            spModifyUserInfo.Visible =true;

            string strUserName=Request.Cookies["soft000"]["UserName"];
            UserInfo info=m_User.GetInfoByUserName(strUserName.Trim());
            if (info != null && info.u_Id != 0 && info.Grade == 1)
            {
                spSettings.Visible = true;
            }
            else
            {
                spSettings.Visible = false;
            }
        }
    }
    #region 更新在线人数
    private void UpdateOnline()
    {
        string strIp = Lib.GetClientIP();
        string strSystem = Lib.GetClientOperation();
        string strUserName = Request.Cookies["soft000"]["UserName"];

        OnLineInfo info = m_Online.GetInfoByIp(strIp);
        //指定的IP如果不在数据库中添加
        if (info == null || info.O_Id == 0)
        {
            OnLineInfo addInfo = new OnLineInfo();
            addInfo.O_Ip = strIp;
            addInfo.O_LanMu = 0;
            addInfo.O_LastTime = DateTime.Now;
            addInfo.O_System = strSystem;
            addInfo.O_UserName = strUserName;
            m_Online.Add(addInfo);
        }
        else
        {
            //更新该用户的最后活动时间
            m_Online.UpdateLastAction(strSystem, strUserName, strIp);
        }
        //删除过期的用户
        m_Online.DeleteTimeOut(BaseConfigs.GetTimeOutMinute);
    }
    #endregion

    #region 检查IP
    public void CheckIp()
    {
        string strIp = Lib.GetClientIP();
        string strLockIp = m_Home.GetLockIp();
        if (string.IsNullOrEmpty(strLockIp))
            return;
        strLockIp = strLockIp.Replace("\r\n", "|").Replace("\r", "|").Replace("\n", "|").Replace(",", "|").Replace(";", "|");
        //销除重复的|
        int i = 0;
        do
        {
            strLockIp = strLockIp.Replace("||", "|");
            i=strLockIp.IndexOf("||");
        } while (i != -1);
        string[] arrLockIp = strLockIp.Split('|');
        foreach (string str in arrLockIp)
        {
            if (IsIpEqual(strIp, str))
            {
                Page.Title = "IP禁址";
                Response.Write("<font color=red><center><br><br>很抱歉,由于您的IP地址或IP地址段被管理员加入了黑名单,<BR>因此您暂时无法浏览论坛和发表内容!<br>有问题请跟管理员联系<br>");
                Response.End();
                return;
            }
        }
    }
    /// <summary>
    /// 判断两学是否相等
    /// </summary>
    /// <param name="strIp"></param>
    /// <param name="strLock"></param>
    /// <returns></returns>
    public bool IsIpEqual(string strIp, string strLock)
    {
        strIp=strIp.Replace("::", ".").Replace(":", ".");
        strLock=strLock.Replace("::", ".").Replace(":", ".");
        string[] arrIp = strIp.Split('.');
        string[] arrLockIp = strLock.Split('.');
        int i = 0;
        for (; i < arrLockIp.Length && i < arrIp.Length; i++)
        {
            if (arrIp[i].Trim() == "*" || arrLockIp[i].Trim() == "*")
                continue;
            if (arrIp[i].Trim().ToLower() != arrLockIp[i].Trim().ToLower())
                return false;
        }
        if (i == arrLockIp.Length && i == arrIp.Length)
            return true;
        return false;
    }
    #endregion

    #region style
    private void LoadStyle()
    {
        int l_id = 0;
        if (Request.QueryString["l_id"] != null)
        {
            l_id = Convert.ToInt32(Request.QueryString["l_id"]);
        }
        StyleCssInfo info = m_Style.GetStyleInfo(l_id);
        if (info != null)
        {
            background = info.BackGround;
            hColor = info.hColor;
            tColor = info.tColor;
            tColor1 = info.tColor1;
            tColor2 = info.tColor2;
            bdtj = info.BDTJ;
            bdtj2 = info.BDTJ2;
            tdc = info.TDC;
            tdc1 = info.TDC1;
            tdc2 = info.TDC2;
            tdc3 = info.TDC3;
            top1 = info.Top1;
            link = info.Link;
            topimg = info.TopImg;
            r_reply = info.r_Reply;
            t_reply = info.t_Reply;
            v_reply = info.v_Reply;
        }
    }
    #endregion
}
